package com.example.demo.controller;

import com.example.demo.entity.Instorage;
import com.example.demo.entity.Outstorage;
import com.example.demo.entity.Storage;
import com.example.demo.mapper.InstorageMapper;
import com.example.demo.mapper.StorageMapper;
import com.example.demo.result.RestBean;
import com.example.demo.service.OutstorageService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/outstorage")
public class OutstorageController {

    @Autowired
    private OutstorageService outstorageService;

    @Autowired
    private InstorageMapper instorageMapper;

    @Autowired
    private StorageMapper storageMapper;


    @PostMapping("/create")
    public RestBean<String> createOutStorage(@RequestBody Outstorage outstorage){
        List<Storage> storages = storageMapper.findAllstorages();
        for (Storage storage : storages) {
            if (outstorage.getOsbelong() == (storage.getSid())){
                if (storage.getStonnum()<outstorage.getOtonnum()){
                    return RestBean.failure(201,"仓库剩余吨数不足");
                } else if (storage.getScasenum()<outstorage.getOcasenum()) {
                    return RestBean.failure(201,"仓库剩余件数不足");
                }
            }
        }
        try {
            outstorage.setOcost(cal_cost(outstorage));
            int result = outstorageService.createOutstorage(outstorage);

            if (result == 1){
                return RestBean.success("添加成功");
            }else{
                return RestBean.failure(201,"添加失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return RestBean.failure(201, "出库失败,请检查取货仓库是否存在或是否仓库余量不足");
        }
    }


    @PostMapping("/delete")
    public RestBean<String> deleteOutstorage(@RequestBody Outstorage outstorage) {

        int result = outstorageService.deleteOutstorage(outstorage.getOid());

        if (result == 1) {
            return RestBean.success("删除成功！");
        }
        return RestBean.failure(404,"删除失败！");
    }


    @PostMapping ("/findone")
    public RestBean<List<Outstorage>> findOutstorage(@RequestBody Outstorage outstorage) {

        List<Outstorage> outstorages =new ArrayList<>(5);
        outstorages.add(outstorageService.findOutstorage(outstorage.getOid()));
        if (outstorages.get(0)==null){
            return RestBean.failure(201,null);
        }
        return RestBean.success(outstorages);
    }


    @GetMapping("/findall")
    public RestBean<List<Outstorage>> findAlloutstorages() {

        List<Outstorage> outstorages = outstorageService.findAlloutstorages();

        if (!outstorages.isEmpty()) {
            System.out.println("查询成功！");
            return RestBean.success(outstorages);
        }
        System.out.println("查询失败！");
        return RestBean.failure(404,null);
    }


    public static long getIntervalDays(Date date1, Date date2) {
        long nd = 1000 * 24 * 60 * 60;
        long diff = date2.getTime() - date1.getTime();
        long day = diff / nd;
        return day + 1;
    }


    public double cal_cost(Outstorage outstorage) throws ParseException {

        List<Instorage> instorages = instorageMapper.findAllinStoreByStorageid(outstorage.getOsbelong());
        double cost = 0;
        double outton = outstorage.getOtonnum();

        for (Instorage instorage : instorages) {
            Instorage newinstorage = instorageMapper.findInstorage(instorage.getIid());
            double realTon = instorageMapper.findRealTon(newinstorage.getIid());

            if (outton == 0) break;

            if (realTon==0) continue;

            double inton = realTon;

            if (inton < outton) {
                outton -= inton;
                double temp = inton;
                inton = 0;
                instorageMapper.updateRealTon(newinstorage.getIid(),inton);
                long days = getIntervalDays(newinstorage.getItime(), outstorage.getOtime());

                if (days >= 15) {
                    cost += temp*0.5*(days-14);
                }
            }else {
                double temp = outton;
                inton -= temp;
                outton = 0;
                instorageMapper.updateRealTon(newinstorage.getIid(),inton);
                long days = getIntervalDays(newinstorage.getItime(), outstorage.getOtime());

                if (days>=15){
                    cost += temp*0.5*(days-14);
                }
            }
        }
        return cost;
    }


}
